<script setup lang="ts">
import Taro, { hideLoading, showLoading } from '@tarojs/taro';
import { storeToRefs } from 'pinia';
import { useNurseUser } from '@/service';
import { useNursesStore, useAuthStore } from '@/store';
import { useGetAuthInfo, useAuthLogin, useNurseAuthLogin, useNurseGetAuthInfo, useGetOpenid } from '@/hooks';
import { localStg } from '@/utils';
import UserHome from './components/userhome.vue';
import NursesHome from './components/nurseshome.vue';

definePageConfig({
  navigationStyle: 'custom',
  navigationBarTitleText: '安好护理',
  enablePullDownRefresh: true
});

const { nurseNurses, openid: nurse_openid } = storeToRefs(useNursesStore());

const { openid, nurseUser } = storeToRefs(useAuthStore());
// import { useGetPhone, useGetOpenid } from '@/hooks';

const { refreshSpecial, getVisibleByHospital } = useNurseUser();

async function initAuth() {
  const openid_Stg = localStg.get('openid');

  if (openid_Stg) {
    showLoading({ title: '正在请求...' });
    openid.value = openid_Stg;
    await useAuthLogin();
    await useGetAuthInfo();
    if (nurseUser.value.id) refreshSpecial(nurseUser.value.id);
    hideLoading();
  }
  else{
    // useGetOpenid()
  }

  const nurseopenid = localStg.get('nurse_openid');
  if (nurseopenid) {
    showLoading({ title: '正在请求...' });
    nurse_openid.value = nurseopenid;
    await useNurseAuthLogin();
    await useNurseGetAuthInfo();
    hideLoading();
  }

  // Taro.login({
  //   success: (res) => {
  //     console.log(res, "res---------=======================");
  //     useGetPhone(res.code);
  //   }
  // });
}

initAuth();
</script>
<template>
  <basic-layout show-tab-bar>
    <NursesHome v-if="nurse_openid && nurseNurses?.id" />
    <UserHome v-if="!nurse_openid && !nurseNurses?.id" />
  </basic-layout>
</template>
